from django.shortcuts import render, redirect
from django.http import HttpResponse,HttpResponseForbidden,JsonResponse
from django.urls import reverse
import json
# Create your views here.


def qs(request):
    return HttpResponse('hello!!')


def qs_hello(request):
    # 反解析 逆向解析 根据路由名称，返回具体的路径
    # reqresp:qs  namespace:name

    # url = reverse('reqresp:qs')
    # print(url)
    # print(request.path)
    # return HttpResponse('qs hello!!')

    # 重定向
    return redirect(reverse('reqresp:qs'))
    # return redirect(reverse('qs'))


# 获取请求行的数据
# 未命名传参
# http://127.0.0.1:8000/demo/36/weather
def demo_view(request, city,weather):
    print(weather)
    print(city)

    return HttpResponse('ok')
# 命名传参
def demo_view(request, city, weather):
    # http://127.0.0.1:8000/demo/36/weather
    print(city)
    print(weather)
    return HttpResponse('ok')


# 获取请求查询的字符串
def get_qs(request):
    # http://127.0.0.1:8000/get_qs/?a=1&b=2&a=3
    # 如果一个键有多个值，获取时取最后一个，即最新的值
    a = request.GET.get('a')
    a_list = request.GET.getlist('a')
    b = request.GET.get('b')
    print(a, a_list, b)
    return HttpResponse('ok')


# 获取表单数据
def get_form_data(request):
    print(request.POST.get('a'))
    print(request.POST.get('b'))
    return HttpResponse('ok')


def get_json_data(request):
    body_data = request.body
    print(type(body_data))
    # 解码
    str_data = body_data.decode()
    print(str_data)
    # dumps() 把dict转化成str
    # loads() 把str转为dict
    data = json.loads(str_data)
    print(data['a'])
    return HttpResponse('ok')


# 获取请求头数据
def get_header(request):
    print(request.method)
    print(request.META)
    # print(request.META['PYTHONIOENCODING'])
    print(request.META['PYTHONABC'])
    # print(request.META['HTTP_ACCEPT_ENCODING'])
    return HttpResponse('ok')


# 响应对象
def get_resp(request):
    """
    响应头Headers:
    Content-Length →13
    Content-Type →text/html; charset=utf-8
    Date →Fri, 29 Jun 2018 05:10:54 GMT
    Server →WSGIServer/0.2 CPython/3.5.2
    X-Frame-Options →SAMEORIGIN

    响应体Body: itcast python
    """
    return HttpResponse('itcast python', status=404)

    """
        postman 测试
        headers:
        Content-Length →0
        Content-Type →text/html; charset=utf-8
        Date →Fri, 29 Jun 2018 04:42:27 GMT
        Server →WSGIServer/0.2 CPython/3.5.2
        X-Frame-Options →SAMEORIGIN
        itcast →python  响应给请求头
    """

    # resp = HttpResponse()
    # resp['itcast'] = 'python'
    # resp.status_code = 404
    # return resp

    """
        响应头Headers:
       Content-Length →29
       Content-Type →application/json
       Date →Fri, 29 Jun 2018 04:56:49 GMT
       Server →WSGIServer/0.2 CPython/3.5.2
       X-Frame-Options →SAMEORIGIN
       
       响应体Body：{"a": 12, "itcast": "python"}
    """

    # return JsonResponse({'a': 12, 'itcast':'python'})

    """
        响应头Headers:
        Content-Length →7
        Content-Type →application/json
        Date →Fri, 29 Jun 2018 05:05:55 GMT
        Server →WSGIServer/0.2 CPython/3.5.2
        X-Frame-Options →SAMEORIGIN
        
        响应体Body：aitcast
    """

    # return HttpResponse({'a': 12, 'itcast': 'python'}, content_type='application/json')

